package com.etc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.etc.entity.CommentResult;
import com.etc.service.BuildingService;
import com.etc.service.ParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ParkController {
    @Autowired
    private ParkService parkService;
    @Autowired
    private BuildingService buildingService;

    @RequestMapping("park/delBatch")
    public CommentResult delBatch(@RequestBody String ids){
        String res = JSON.toJSON(ids).toString();
        List<Integer> delIds = JSONArray.parseArray(res,Integer.class);
        for (int i = 0; i < delIds.size(); i++) {
            System.out.println(delIds.get(i));

            if (buildingService.findBuildingByPark(delIds.get(i)).size()>0){
                String park_name = parkService.findById(delIds.get(i)).getPark_name();
                return new CommentResult(444,"需要先将"+park_name+"下的楼栋移除或转让");
            }
        }
        int result = parkService.delBatch(delIds);
        if (result>0){
            return new CommentResult(200,"删除成功");
        }
        return new CommentResult(444,"failed");
    }

}
